- 
                Notifications
    
You must be signed in to change notification settings  - Fork 2.7k
 
feat(log): add custom variables for enhanced logging capabilities #12696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
f720c7f    to
    0d9c299      
    Compare
  
    | 
           We have some built-in variables and all nginx variables are supported. Can you explain in detail why this change is needed? https://apisix.apache.org/docs/apisix/apisix-variable/  | 
    
| 
           The goal here is to give users more freedom and control over how logs are handled. Right now, the default log includes useful fields but it also bundles headers and other request/response data that I don’t want to store due to privacy concerns and storage overhead. I want to be able to: 
 Customize log formats based on destination and purpose To do that, I need those key fields (latency, hostname, method, version, client IP, etc.) to be available in log_format, so I can use them in custom_log without relying on the default log structure. This change enables that flexibility while keeping observability intact and avoiding unnecessary data. Let me know if you have concerns or suggestions  | 
    
| 
           Hi @felpasl, we support customizing log formats directly in the log plugin, and support all built-in variables. Does this not meet your needs? https://apisix.apache.org/docs/apisix/plugins/kafka-logger/#metadata  | 
    
| 
           Thanks for the follow-up. Just to clarify: the change includes methods and calculations that aren't available through existing NGINX or APISIX variables. For example:     latency = function() return (ngx_now() - ngx.req.start_time()) * 1000 end,
    upstream_latency = function(ctx)
        return ctx.var.upstream_response_time and ctx.var.upstream_response_time * 1000 or nil
    end,
    apisix_latency = function(ctx)
        local latency = (ngx_now() - ngx.req.start_time()) * 1000
        local upstream_latency =
            ctx.var.upstream_response_time and ctx.var.upstream_response_time * 1000 or 0
        local apisix_latency = latency - upstream_latency
        if apisix_latency < 0 then apisix_latency = 0 end
        return apisix_latency
    end,
    hostname = function() return core.utils.gethostname() end,
    version = function() return core.version.VERSION end,These aren't just references to existing variables — they encapsulate logic that calculates values like total latency, APISIX processing time, and upstream latency. My proposal is to expose these as named variables so users can access them directly in log_format, without needing to replicate the logic themselves. While many fields (like client_ip, request_method, etc.) are already covered by NGINX variables but with other names and having them available as variables would simplify custom logging and improve consistency of naming variables between default log and log_format across plugins.  | 
    
Description
Introduced a comprehensive set of custom logging variables as functions, including:
Simplified the function to use the custom_vars table for dynamic variable resolution, replacing hardcoded logic for request_body with a generic approach.
Added group_id to the consumer object in full log entries.
Which issue(s) this PR fixes:
Fixes #
Checklist